package com.laot.searchManage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laot.searchManage.mapper.DatasourceApplicationMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.laot.seacher_master.model.common.ErrorCode;
import org.laot.seacher_master.model.common.PageRequest;
import org.laot.seacher_master.model.exception.BusinessException;
import org.laot.searchManage.dto.DatasourceApplicationApproveDto;
import org.laot.searchManage.dto.DatasourceApplicationCommitDto;
import org.laot.searchManage.entry.DatasourceApplication;
import org.laot.searchManage.service.IDatasourceApplicationService;
import org.laot.searchManage.vo.DatasourceApplicationPageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author laoT
* @description 针对表【datasource_application(数据源申请单)】的数据库操作Service实现
* @createDate 2024-01-24 22:08:30
*/
@Service
public class DatasourceApplicationServiceImpl extends ServiceImpl<DatasourceApplicationMapper, DatasourceApplication>
    implements IDatasourceApplicationService {
    @Resource
    private DatasourceApplicationMapper datasourceApplicationMapper;

    @Override
    public void commit(DatasourceApplicationCommitDto dto) {
        //对数据精选校验
        if(dto == null) throw new BusinessException(ErrorCode.PARAMS_ERROR,"传入参数不能为空");
        if(StringUtils.isBlank(dto.getTitle())) throw new BusinessException(ErrorCode.PARAMS_ERROR,"标题不能为空");
        if(dto.getType() == null) throw new BusinessException(ErrorCode.PARAMS_ERROR,"数据源类型不能为空");
        if(StringUtils.isBlank(dto.getAuthorEmail())) throw new BusinessException(ErrorCode.PARAMS_ERROR,"作者邮箱不能为空");
        if(StringUtils.isBlank(dto.getCodeUrl())) throw new BusinessException(ErrorCode.PARAMS_ERROR,"说明文档不能为空");
        DatasourceApplication datasourceApplication = new DatasourceApplication();
        BeanUtils.copyProperties(dto,datasourceApplication);
        //进行保存
        this.baseMapper.insert(datasourceApplication);
    }

    @Override
    public void approve(DatasourceApplicationApproveDto datasourceApplicationApproveDto, String userName) {
        if(datasourceApplicationApproveDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(datasourceApplicationApproveDto.getId() == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if(StringUtils.isBlank(datasourceApplicationApproveDto.getApproveIdea())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请输入审批意见！");
        }
        //根据id获取数据源申请单
        DatasourceApplication datasourceApplication = this.getById(datasourceApplicationApproveDto.getId());
        if(datasourceApplication == null) throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"数据源申请单不存在！");
        datasourceApplication.setApproveStatus(datasourceApplicationApproveDto.getApproveStatus());
        datasourceApplication.setApproveIdea(datasourceApplicationApproveDto.getApproveIdea());
        datasourceApplication.setApproveTime(new Date());
        datasourceApplication.setApproveUserName(userName);
        this.baseMapper.updateById(datasourceApplication);
    }

    @Override
    public Page<DatasourceApplicationPageVo> pageInfo(PageRequest pageRequest) {
        if(pageRequest == null) throw new BusinessException(ErrorCode.PARAMS_ERROR);
        if(pageRequest.getPageSize() == 0){
            pageRequest.setPageSize(20);
        }
        if(pageRequest.getCurrent() == 0){
            pageRequest.setCurrent(1);
        }
        Page<DatasourceApplicationPageVo> pageVoPage = new Page<>(pageRequest.getCurrent(), pageRequest.getPageSize());
        Page<DatasourceApplication> applicationPage = new Page<>(pageRequest.getCurrent(), pageRequest.getPageSize());
        this.page(applicationPage);
        pageVoPage.setTotal(applicationPage.getTotal());
        //填充返回的对象
        List<DatasourceApplication> records = applicationPage.getRecords();
        if(CollectionUtils.isNotEmpty(records)){
            List<DatasourceApplicationPageVo> list = new ArrayList<>();
            for (DatasourceApplication datasourceApplication : records) {
                DatasourceApplicationPageVo vo = new DatasourceApplicationPageVo();
                BeanUtils.copyProperties(datasourceApplication,vo);
                list.add(vo);
            }
            pageVoPage.setRecords(list);
        }
        return pageVoPage;
    }
}




